package com.cuitrip.im.proxy;

import com.cuitrip.app.rong.RongCloudEvent;
import com.cuitrip.im.CtInfoNotificationMsgItemProvider;
import com.cuitrip.im.CtParam;
import com.cuitrip.im.model.CTInformationNotificationMessage;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;

import java.util.HashMap;

import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;

/**
 * 系统消息通知代理类
 * Created by ak on 15/9/23.
 */
public class CtRongInfoProxy {


    public static final boolean RONG_MSG_SEND_OPEN = true;
    public static final boolean RONG_MSG_RECEIVE_OPEN = true;
    private static CtRongInfoProxy sInstance;


    public static CtRongInfoProxy getInstance() {
        if (null == sInstance)
            sInstance = new CtRongInfoProxy();
        return sInstance;
    }

    public CTInformationNotificationMessage getCommonOrderChangeMessage(CtInfoNotificationMsgItemProvider.CtParamKey ctParamKey,
                                                                        String nickName, long timeMills, String date) {
        String content;
        if (date == null) {
            content = ResourceUtil.getString(ctParamKey.commonRes, nickName);
        } else {
            content = ResourceUtil.getString(ctParamKey.commonRes, nickName, date);
        }
        HashMap<String, String> map = new HashMap<>();
        map.put(CtParam.NICK_KEY, nickName);
        if (timeMills != -1) {
            map.put(CtParam.DATE_KEY, String.valueOf(timeMills));
        }
        CTInformationNotificationMessage message = CtInfoNotificationMsgItemProvider.build(content, ctParamKey, map);
        return message;

    }

    public CTInformationNotificationMessage getModifyOrderMessage(String nickName, int personNum, long timeMills, String date) {
        String content;
        CtInfoNotificationMsgItemProvider.CtParamKey ctParamKey;
        if (personNum > 1) {
            content = ResourceUtil.getString(R.string.BOOK_MSG_MODIFY_MORE_PRE);
            ctParamKey = CtInfoNotificationMsgItemProvider.CtParamKey.BOOK_MSG_MODIFY_MORE;
        } else {
            content = ResourceUtil.getString(R.string.BOOK_MSG_MODIFY_ONE_PRE);
            ctParamKey = CtInfoNotificationMsgItemProvider.CtParamKey.BOOK_MSG_MODIFY_ONE;
        }
        content = String.format(content, nickName, date, String.valueOf(personNum));

        HashMap<String, String> map = new HashMap<>();
        map.put(CtParam.NICK_KEY, nickName);
        map.put(CtParam.DATE_KEY, String.valueOf(timeMills));
        map.put(CtParam.PERSON_KEY, String.valueOf(personNum));
        CTInformationNotificationMessage message = CtInfoNotificationMsgItemProvider.build(content, ctParamKey, map);
        return message;
    }

    public void sendSystemMessage(String targetId, CTInformationNotificationMessage message, RongIMClient.SendMessageCallback callback) {
        if (!RONG_MSG_SEND_OPEN) {
            return;
        }
        RongCloudEvent.getInstance().sendMessage(Conversation.ConversationType.DISCUSSION, targetId, message, null, null, callback);
    }

}
